//
//  InformationViewController.swift
//  SafeFoodManagerDemo
//
//  Created by RunDeXian on 2018/6/26.
//  Copyright © 2018年 bob. All rights reserved.
//

import UIKit

class InformationViewController: UIViewController {
    var Tpview:TypeView? = TypeView()
    var Datatype:REQuestDataType = REQuestDataType(rawValue: 0)!{
        didSet{
            
            if Datatype.rawValue == 0 {
                
                Tpview?.text = "没有任何数据"
                Tpview?.imageName = "emptynoData"
                self.view.addSubview(self.Tpview!)
            }
            
            if Datatype.rawValue == 1 {
                self.Tpview?.removeFromSuperview();
                
            }
        }
    }
    var data:[Categorys] = [Categorys](){
        didSet{
            var arrayC:[String] = [String]()
            for cat in data{
                arrayC.append(cat.name!)
                
                
            }
            titles = arrayC
            
        }
        
        
        
    }
    var titles:[String]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.Tpview?.frame = self.view.bounds
        loadData()
        //self.navigationController?.navigationBar.isHidden = true
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //加在数据
    func loadData(){
        
        
        let params = NSMutableDictionary()
        let API:BaseReqApi = BaseReqApi.init(requestUrl: "/index.php/FoodCheckApi/courseCatList.json", andrequestTime: 5, andParams: params as! [AnyHashable : Any], andRequestMethod: YTKRequestMethod.POST, andCache: false, andCacheTime: 0, andPostToken: true)
        
        API.starRequest { (status, mess, response) in
            
            guard let dic:Dictionary = response as? [String :AnyObject] else{
                
                return
            }
            print(dic)
            
            if status.rawValue == 1  {
                let mode1  = CateMode.init(DIC: dic )
                self.data = mode1.data as! [Categorys]
                
                if self.data.count == 0{
                    self.Datatype = REQuestDataType.NODATAType
                    
                }else{
                    self.Datatype = REQuestDataType.NORMALLType;
                    self.setUpUI()
                }
                
                
                
                
                
            }else if (status.rawValue == 404){
                
                UIAlertView.bk_show(withTitle: mess, message: "", cancelButtonTitle: "登陆", otherButtonTitles: ["取消"], handler: { (alertView, buttonIndex) in
                    if(buttonIndex == 1){
                        
                        
                        self.gotoLogin()
                        
                    }
                })
                
                
            }
        }
        print(API)
    }
    
    func setUpUI(){
        
        //创建主题内容。
        let style = ZXTitleStytle()
        
        let pageFrame = CGRect(x: 0, y: kNavigationBarH, width: kScreenW, height: kScreenH - kNavigationBarH - kStatusBarH)
        var childVcs:[InformationListTableViewController] = [InformationListTableViewController]()
        
        for mode in data{
            //vc.view.backgroundColor = UIColor.randomColor();
            let vc = InformationListTableViewController()
            vc.categ = mode
            //vc.navigationController?.navigationBar.barTintColor = UIColor.orange
            childVcs.append(vc)
        }
        
        let pageView = ZXPageView(frame: pageFrame, titles: titles!, style: style, childVcs: childVcs, parentVc: self)
        
        view.addSubview(pageView)
        
    }
    
    
}
